*********************************************************************
***           COMPARE COMPARISON GROUP VS. OVERALL          ***
*** Date modified: 8/29/2022                                ***
*********************************************************************

cap mkdir "${OutputPath}/Overall vs Border/"
log using "${CodePath}/log/compare_hospcompgroup_overall_$S_DATE.log", text replace
*************
* 100M RADIUS HOSPITAL COMPARISON GROUP
*************

local hospvars    "`hospvars' beds urgeo_en forprofit independent totcost real_gcost_admin_net n_claims tot_pmt_amt sh_claims_02 pred_0709_aud2011"

use "${DataPath}/within100m/pnlist_groups_100m.dta", clear
gen sample_hosp_100mradius = 1
duplicates drop hospcomp_group_100 pn, force
expand 9
sort pn
bys pn hospcomp_group_100 : gen fyear = _n + 2006
// create a small dataset with relevant vars
preserve
	use pn fyear dist_to_border jk_state_audit_in2011* audit_in2011* jk_RACregion_audit_in2011* `hospvars' nearseg100_state_2 nearseg100_state RACregion state RACregion_audit_in2011 state_audit_in2011 using "${DataPath}/derived/hospyear_0716_jk.dta", clear
  keep if !missing(RACregion_audit_in2011) // restrict to acute hospitals
	keep if fyear >= 2007 & fyear <= 2015

  distinct pn
  cap drop sample_total
  gen sample_total = 1

  bys pn: egen pn_min = min(fyear)
  bys pn: egen pn_max = max(fyear)
  bys pn: egen pn_nyears = nvals(fyear)

  replace sample_total = 0 if pn_min > 2007
  replace sample_total = 0 if pn_max < 2014
  replace sample_total = 0 if pn_nyears < 8


	tempfile temp
	save `temp'
restore

merge m:1 pn fyear using `temp', keep(1 3) gen(_jkmerge)
replace sample_hosp_100mradius = 0 if missing(sample_hosp_100mradius)

label var state_audit_in2011 "2011 state audit rate"
label var audit_in2011 "2011 audit rate"
*label var wageindex "wage index"

egen hospcomp_group_fyear = group(hospcomp_group_100 fyear)
egen state_splitid = group(state split_id)
egen state_nearseg100 = group(state nearseg100_state)

// create an indicator for being on the "high" side of each comparison group (in the region with the highest audit rate)
	bys hospcomp_group_100 RACregion: egen group_RACregion_auditrate = mean(jk_state_audit_in2011)
	bys hospcomp_group_100: egen max_group_auditrate = max(group_RACregion_auditrate)

	gen 	highRACside = 0
	replace highRACside = 1 if group_RACregion_auditrate == max_group_auditrate
  label var highRACside "high audit side of border"

gen urban = urgeo_en == 1 | urgeo_en == 2
gen tot_pmt_amt_1e6 = tot_pmt_amt/1000000
gen admin_net_1e6 = real_gcost_admin_net/1000000
gen totcost_1e6 = totcost/1000000

gen sample_border = sample_hosp_100mradius == 1 & n_hospcomp_ingroup_100 != 1


// audit rate regression
  eststo clear

  foreach var of varlist beds urban forprofit independent totcost_1e6 admin_net_1e6 n_claims tot_pmt_amt_1e6 sh_claims_02 pred_0709_aud2011{
    reghdfe `var' audit_in2011 if fyear == 2010 & sample_hosp_100mradius == 1, absorb(hospcomp_group_100) cluster(state)
    eststo m1_`var'
    distinct pn if e(sample)
    estadd local hosp = r(ndistinct)

    sum `var' if e(sample)
    local avg = r(mean)
    local avg : di %3.2f `avg'
    di "`avg'"
    estadd local mean = `avg'

  }

label var beds                        "beds"
label var urban                       "urban"
label var forprofit                   "for profit"
label var independent                 "non-chain"
label var sh_claims_02                "\shortstack{short stay share}"
label var n_claims                    "\shortstack{Medicare\\admissions}"
label var tot_pmt_amt_1e6             "\shortstack{inpatient\\revenue \\(millions)}"
label var admin_net_1e6               "\shortstack{admin\\costs\\(millions)}"
label var pred_0709_aud2011                "\shortstack{predicted 2011\\audit rate}"

  esttab  using "${OutputPath}/Overall vs Border/border_100m_audit.tex", ///
  stats(hosp mean, labels("N Hosp" "Mean")  fmt(%9.0g)) ///
    b(%9.2f) keep(audit_in2011) label ///
    note("Clustered at state level") ///
    starlevels(* 0.10 ** 0.05 *** 0.01) se replace 

*************
* OVERALL
*************
use "${DataPath}/derived/hospyear_0716_jk.dta", clear
keep if !missing(RACregion_audit_in2011) // restrict to acute hospitals
gen RACregion2 = "A" if RACregion ==1
replace RACregion2 = "B" if RACregion == 2
replace RACregion2 = "C" if RACregion == 3
replace RACregion2 = "D" if RACregion == 4

label var state_audit_in2011 "2011 state audit rate"
label var audit_in2011 "2011 audit rate"
label var wageindex "wage index"
label var log_tot_pmt_amt "log inpatient revenue"
label var audit_in2011 "2011 audit rate"


gen racAD = RACregion == 1 | RACregion == 4
label var racAD "RAC regions A or D (high rate regions)"

gen tot_pmt_amt_1e6 = tot_pmt_amt/1000000
gen admin_net_1e6 = real_gcost_admin_net/1000000
gen totcost_1e6 = totcost/1000000


eststo clear

foreach var of varlist beds urban forprofit independent totcost_1e6 admin_net_1e6 n_claims tot_pmt_amt_1e6 sh_claims_02 pred_0709_aud2011{
  reg `var' audit_in2011 if fyear == 2010 & sample_total == 1, cluster(state)
  eststo m2_`var'
  distinct pn if e(sample)
  estadd local hosp = r(ndistinct)

  sum `var' if e(sample)
  local avg = r(mean)
  local avg : di %3.2f `avg'
  di "`avg'"
  estadd local mean = `avg'
}

label var beds                        "beds"
label var urban                       "urban"
label var forprofit                   "for profit"
label var independent                 "non-chain"
label var sh_claims_02                "\shortstack{short stay share}"
label var n_claims                    "\shortstack{Medicare\\admissions}"
label var tot_pmt_amt_1e6             "\shortstack{inpatient\\revenue \\(millions)}"
label var admin_net_1e6               "\shortstack{admin\\costs\\(millions)}"
label var pred_0709_aud2011                 "\shortstack{predicted 2011\\audit rate}"


  esttab  using "${OutputPath}/Overall vs Border/overall_audit.tex", ///
  stats(hosp mean, labels("N Hosp" "Mean") fmt(%9.0g)) ///
    b(%9.2f) keep(audit_in2011) label ///
    note("Clustered at state level") ///
    starlevels(* 0.10 ** 0.05 *** 0.01) se replace 

  log close

